package rs.dalao.syc.doctor.all.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Service;
import rs.dalao.syc.common.ex.ServiceException;
import rs.dalao.syc.common.pojo.authentication.CurrentPrincipal;
import rs.dalao.syc.common.response.ServiceCode;
import rs.dalao.syc.doctor.all.dao.persist.repository.IArticleRepository;
import rs.dalao.syc.doctor.all.pojo.entity.Article;
import rs.dalao.syc.doctor.all.pojo.param.ArticleParam;
import rs.dalao.syc.doctor.all.pojo.param.ArticleUpdateParam;
import rs.dalao.syc.doctor.all.pojo.vo.ArticleManagementVO;
import rs.dalao.syc.doctor.all.pojo.vo.ArticleStandardVO;
import rs.dalao.syc.doctor.all.pojo.vo.ArticleValidateVO;
import rs.dalao.syc.doctor.all.service.IArticleService;

import java.util.List;


@Service
@Slf4j
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private IArticleRepository articleRepository;

    @Override
    public int insertArticle(ArticleParam articleParam, @AuthenticationPrincipal CurrentPrincipal currentPrincipal){
        log.debug("开始处理【发表文章】的业务，参数：{}", articleParam);
        ArticleValidateVO articleValidateVO = articleRepository.getArticleByTitle(articleParam.getTitle());
        if (articleValidateVO != null) {
            String message = "发表文章失败，已存在改关键词文章！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_POST_ARTICLE_REPEAT,message);
        }
        Article Article = new Article();
        BeanUtils.copyProperties(articleParam, Article);
        Article.setViewCount(0);
        Article.setAuthor(currentPrincipal.getId());
        articleRepository.insertArticle(Article);
        return 1;
    }

    @Override
    public int deleteByArticleId(Long id) {
        log.debug("开始处理【根据ID删除文章】的业务，参数：{}", id);
        if (id == 1) {
            String message = "删除文章失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        Article articleById = articleRepository.getArticleById(id);
        if (articleById == null) {
            String message = "删除文章失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int i = articleRepository.deleteArticleByArticleId(id);
        return i;
    };

    @Override
    public Article getArticleById(Long id) {
        return articleRepository.getArticleById(id);
    }

    @Override
    public List<ArticleManagementVO> getAllArticleByAuthorId(Long authorId) {
        log.debug("开始处理【查询所有文章的业务】");
        return articleRepository.getAllArticleByAuthorId(authorId);
    }

    @Override
    public void updateInfoById(Long  id, ArticleUpdateParam articleUpdateParam) {
        log.debug("开始处理【修改类别详情】的业务，ID：{}，新数据：{}", id, articleUpdateParam);
        Article queryResult = articleRepository.getArticleById(id);
        if (queryResult == null) {
            String message = "修改文章失败，尝试修改的文章数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        Article article = new Article();
        BeanUtils.copyProperties(articleUpdateParam, article);
        article.setId(id);
        int rows = articleRepository.updateArticle(article);
        if (rows != 1) {
            String message = "修改文章失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public int updateViewCountById(Long id) {
        log.debug("开始处理浏览量更改业务"+id);
        Article articleById = articleRepository.getArticleById(id);
        if(articleById==null){
            String message = "浏览量增加失败，尝试访问的文章不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }

        return articleRepository.updateViewCountById(id);
    }
}
